<?php 
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP6_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: 牧羊人 <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\model;

use think\Exception;
use think\facade\Db;

/**
 * 商品-模型
 * @author 小树林
 * @since: 2021/11/01
 * Class Product
 * @package app\admin\model
 */
class Product extends BaseModel
{
    // 设置数据表名
    protected $name = "product";

    /**
     * 获取缓存信息
     * @param int $id 记录ID
     * @return \app\common\model\数据信息|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 小树林
     * @since: 2021/11/01
     */
    public function getInfo($id)
    {
        $info = parent::getInfo($id); // TODO: Change the autogenerated stub
        if ($info) {
                                                                                                    
            // 封面图
            $images = explode(',',$info['image']);
            $info['image'] = [];
            $info['image'] = get_image_url($images);

                    
            // 轮播图
            $images = explode(',',$info['images']);
            $info['images'] = [];
            $info['images'] = get_image_url($images);

            //获取规格
            $attribute = Db::name('product_sku')->where('product_id',$info['id'])->group('key,value')->select()->toArray();
            if($attribute){
                $result = [];
                foreach ($attribute as $value) {
                    $result[$value['key']][] = $value;
                }
                $arr = [];
                foreach($result as $k1=>$v1){
                    $item = [];
                    $id = [];
                    foreach($v1 as $k2=>$v2){
                        $item[] = (string)$v2['value'];
                        $id[] = $v2['id'];
                    }
                    $arr[] = ['name'=>(string)$k1,'item'=>$item,'id'=>$id];
                }
                $info['attribute'] = $arr;
            }else{
                $info['attribute'] =[];
            }

            //获取规格详情
            $sku = Db::name('product_sku_detail')->field('id,image,market_value,selling_price,stock,sku')->where('product_id',$info['id'])->select()->toArray();
            $info['sku'] = $sku;
            /*
             * 获取会员规格
             * */
            $attribute1 = Db::name('product_sku1')->where('product_id',$info['id'])->group('key,value')->select()->toArray();
            if($attribute1){
                $result1 = [];
                foreach ($attribute1 as $value1) {
                    $result1[$value1['key']][] = $value1;
                }
                $arr1 = [];
                foreach($result1 as $k11=>$v11){
                    $item1 = [];
                    $id1 = [];
                    foreach($v11 as $k21=>$v21){
                        $item1[] = (string)$v21['value'];
                        $id1[] = $v21['id'];
                    }
                    $arr1[] = ['name'=>(string)$k11,'item'=>$item1,'id'=>$id1];
                }
                $info['attribute1'] = $arr1;
            }else{
                $info['attribute1'] =[];
            }
            //获取规格详情
            $sku1 = Db::name('product_sku_detail1')->field('id,image,market_value as market_value1,selling_price as selling_price1,stock as stock1,sku')->where('product_id',$info['id'])->select()->toArray();
            $info['sku1'] = $sku1;



        }
        return $info;
    }


    public function edit($data = [], &$error = '', $is_sql = false){
        Db::startTrans();
        try {
            $id = $data['id'] ?? '';
            $attribute = $data['attribute'];
            $sku = $data['sku'];

            $attribute1 = $data['attribute1'];
            $sku1 = $data['sku1'];

            unset($data['attribute']);
            unset($data['sku']);
            unset($data['attribute1']);
            unset($data['sku1']);


            $pid = Db::name('product_type')->where('id',$data['type_id'])->value('pid');
            $data['type_one'] = $pid;
            $res = parent::edit($data,$error,$is_sql);

            if($res != false){

                if($id){
                    //修改
                    if($attribute){
                        //规格
                        Db::name('product_sku')->where('product_id',$id)->delete();
                        foreach($attribute as $k=>$v){
                            foreach($v['item'] as $k1=>$v1){
                                Db::name('product_sku')->insert([
                                    'product_id'=>$id,
                                    'key'=>$v['name'],
                                    'value'=>$v1
                                ]);
                            }
                        }
                        //规格详情
                        $ids = Db::name('product_sku_detail')->where('product_id',$id)->column('id');
                        foreach($sku as $ke=>$ve){
                            if($ve['id'] == 0){
                                Db::name('product_sku_detail')->insert([
                                    'product_id'=>$id,
                                    'market_value'=>$ve['market_value'],
                                    'selling_price'=>$ve['selling_price'],
                                    'image'=>$ve['image'],
                                    'stock'=>$ve['stock'],
                                    'sku'=>$ve['sku']
                                ]);
                            }else{
                                Db::name('product_sku_detail')->where('id',$ve['id'])->update([
                                    'market_value'=>$ve['market_value'],
                                    'selling_price'=>$ve['selling_price'],
                                    'image'=>$ve['image'],
                                    'stock'=>$ve['stock'],
                                    'sku'=>$ve['sku']
                                ]);
                                unset($ids[array_keys($ids,$ve['id'])[0]]);
                            }
                        }
                        if(!empty($ids)){
                            Db::name('product_sku_detail')->where('id','in',$ids)->delete();
                        }
                    }
                    if($attribute1){
                        //规格
                        Db::name('product_sku1')->where('product_id',$id)->delete();
                        foreach($attribute1 as $k1=>$v11){
                            foreach($v11['item'] as $k11=>$v111){
                                Db::name('product_sku1')->insert([
                                    'product_id'=>$id,
                                    'key'=>$v11['name'],
                                    'value'=>$v111
                                ]);
                            }
                        }


                        //规格详情
                        $ids1 = Db::name('product_sku_detail1')->where('product_id',$id)->column('id');

                        foreach($sku1 as $ke1=>$ve1){

                            if($ve1['id'] == 0){
                                Db::name('product_sku_detail1')->insert([
                                    'product_id'=>$id,
                                    'market_value'=>$ve1['market_value1'],
                                    'selling_price'=>$ve1['selling_price1'],
                                    'image'=>$ve1['image'],
                                    'stock'=>$ve1['stock1'],
                                    'sku'=>$ve1['sku']
                                ]);
                            }else{
                                Db::name('product_sku_detail1')->where('id',$ve1['id'])->update([
                                    'market_value'=>$ve1['market_value1'],
                                    'selling_price'=>$ve1['selling_price1'],
                                    'image'=>$ve1['image'],
                                    'stock'=>$ve1['stock1'],
                                    'sku'=>$ve1['sku']
                                ]);
                                unset($ids1[array_keys($ids1,$ve1['id'])[0]]);
                            }

                        }
                        if(!empty($ids1)){
                            Db::name('product_sku_detail1')->where('id','in',$ids1)->delete();
                        }
                    }


                }else{
                    //新增
                    if($attribute){

                        //规格
                        foreach($attribute as $k=>$v){
                            foreach($v['item'] as $k1=>$v1){
                                Db::name('product_sku')->insert([
                                    'product_id'=>$res,
                                    'key'=>$v['name'],
                                    'value'=>$v1
                                ]);
                            }
                        }

                        //规格详情
                        foreach($sku as $ke=>$ve){
                            Db::name('product_sku_detail')->insert([
                                'product_id'=>$res,
                                'market_value'=>$ve['market_value'],
                                'selling_price'=>$ve['selling_price'],
                                'image'=>$ve['image'],
                                'stock'=>$ve['stock'],
                                'sku'=>$ve['sku']
                            ]);
                        }
                    }

                    if($attribute1){

                        //规格
                        foreach($attribute1 as $k1=>$v11){
                            foreach($v11['item'] as $k11=>$v111){
                                Db::name('product_sku1')->insert([
                                    'product_id'=>$res,
                                    'key'=>$v11['name'],
                                    'value'=>$v111
                                ]);
                            }
                        }
                        //规格详情
                        foreach($sku1 as $ke1=>$ve1){
                            Db::name('product_sku_detail1')->insert([
                                'product_id'=>$res,
                                'market_value'=>$ve1['market_value1'],
                                'selling_price'=>$ve1['selling_price1'],
                                'image'=>$ve1['image'],
                                'stock'=>$ve1['stock1'],
                                'sku'=>$ve1['sku']
                            ]);
                        }
                    }


                }
            }
            Db::commit();
            return true;
        }catch (Exception $e){
            Db::rollback();
            $error = $e->getMessage();
            return false;
        }
    }


    public function productType(){
        return $this->hasOne(ProductType::class,'id','type_id');
    }

}